from psi.server import combine
from conf import conf
import shutil
import os


def move_files(dir_path):
    config = conf.read_json()
    ks =  bytes.fromhex(config.get('select_key'))
    sel = []
    for i, b in enumerate(ks):
        for j in range(8):
            sel.append((b >> (7 - j)) & 0x01)
    for i in range(128):
        if sel[i] == 0:
            shutil.move(dir_path+f"/t0/{str(i+1)}.bin", dir_path+f"/q/{str(i+1)}.bin")
        else:
            shutil.move(dir_path+f"/t1/{str(i+1)}.bin", dir_path+f"/q/{str(i+1)}.bin")

def work():
    base_dir = os.path.dirname(os.path.abspath(__file__)).replace('\\', '/') + "/../../data/server"
    move_files(base_dir)
    combine.combine_bits(base_dir+"/q/", base_dir+"/q.bin")

if __name__ == '__main__':
    work()